package com.coocaa.cirrus.common.base.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author bijiahao
 * @date : 2025/9/18 09:49
 * @description
 */
@Slf4j
@UtilityClass
public class DateUtil {
    private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat();
        }
    };


    public static long toTimestamp(String str) {
        return toTimestamp(str, "yyyy-MM-dd HH:mm:ss");
    }

    public static long toTimestamp(String str, String pattern) {
        if (StringUtils.isBlank(str)) {
            return 0;
        }
        try {
            if (str.contains("T")) {
                // ISO
                return Instant.parse(str).toEpochMilli();
            }
            DateTimeFormatter f = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(str, f)
                    .atZone(ZoneId.systemDefault())
                    .toInstant()
                    .toEpochMilli();
        } catch (Exception e) {
            log.error("toTimestamp", e);
            return 0;
        }
    }

    public static String timestampToString(long timestamp) {
        if (timestamp <= 0) {
            return "";
        }
        Date date = new Date(timestamp);
        return toLongString(date);
    }

    public static String getCurDateStr(String dateFormat) {
        return format(getCurDate(), dateFormat);
    }

    public static Date getCurDate() {
        return new Date();
    }

    public static String toLongString(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getPreviouDate0Hour(Date current, int days) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        //把当前时间赋给日历
        calendar.setTime(current);
        calendar.add(Calendar.DAY_OF_MONTH, -days); //设置为前一天
        Date dBefore = calendar.getTime(); //得到前一天的时间
        String defaultStartDate = toLongString(dBefore); //格式化前一天
        defaultStartDate = defaultStartDate.substring(0, 10) + " 00:00:00";
        return defaultStartDate;
    }

    public static String getPreviouDate24Hour(Date current, int days) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        //把当前时间赋给日历
        calendar.setTime(current);
        calendar.add(Calendar.DAY_OF_MONTH, -days); //设置为前一天
        Date dBefore = calendar.getTime(); //得到前一天的时间
        String defaultStartDate = toLongString(dBefore); //格式化前一天
        defaultStartDate = defaultStartDate.substring(0, 10) + " 23:59:59";
        return defaultStartDate;
    }

    public static Date getToday0Hour(Date current) {
        Calendar calendar = Calendar.getInstance();
        //把当前时间赋给日历
        calendar.setTime(current);
        Date dBefore = calendar.getTime();
        String defaultStartDate = toLongString(dBefore);
        defaultStartDate = defaultStartDate.substring(0, 10) + " 00:00:00";
        return DateUtil.parseStr2Date(defaultStartDate);
    }

    public static Date getPreviouDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -days);
        date = calendar.getTime();
        return date;
    }

    public static Date getPreviouMinute(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -minutes);
        return calendar.getTime();
    }

    public static Date parseStr2Date(String dateStr) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        try {
            return (new SimpleDateFormat(pattern)).parse(dateStr);
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String toShortString(Date date) {
        return format(date, "yyyy-MM-dd");
    }


    private static String format(Date date, String pattern) {
        SimpleDateFormat sdf = threadLocal.get();
        sdf.applyPattern(pattern);
        return sdf.format(date);
    }

    public static void main(String[] args) {
        System.out.println(timestampToString(System.currentTimeMillis()));
    }
}
